package dao.stems;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import valueObject.stem.Stem;

import com.gabrielfernandes.database.client.DatabaseClient;

import com.mysql.jdbc.interceptors.ResultSetScannerInterceptor;

import model.stems.StemsModelInterface;


public class StemsDAO implements StemsModelInterface {
	private DatabaseClient databaseClient;

	public StemsDAO(DatabaseClient databaseClient) { 
		this.databaseClient = databaseClient;
	}

	public List<Stem> getSteamses(ResultSet resultSetSteams) {
		List<Stem> stems = new ArrayList<Stem>();
		try {
			while(resultSetSteams.next()) {
				Stem stem = new Stem(
						resultSetSteams.getInt("id_pesquisador"),
						resultSetSteams.getString("word"),
						resultSetSteams.getDouble("qtde")
					);
				stems.add(stem);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return stems;
	}	
	
	@Override
	public List<Stem> getSteams() {
		Statement st = null;
		ResultSet rs = null;
		
		List<Stem> stems = null;
		
		try {
			st = this.databaseClient.getClientDatabaseConnection().getDatabaseConnection().createStatement();
			
			rs = st.executeQuery("SELECT * FROM Lattes_Pesquisador_Word_Occur");
			stems = this.getSteamses(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				databaseClient.getClientDatabaseConnection().closeDatabaseConnection();
				st.close();
				rs.close();
				} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return stems;
	}
	
}
